<#--
List of variables to be passed for this template:
        +- rootPackage
        +- applicationName
        +- classNames
        +- directory
        +- Entities: ArrayList of EntityClass.getAsMap()

            |
            +- imports: imports
            |
            +- name: The name of the class.
            |    |
            |    +- single
            |    +- plural
            |
            +- idColumn: the id column
            |    |
            |    +- field
            |    |    |
            |    |    +- name
            |    |    +- type
            |    |
            |    +- getter
            |    +- setter
            |
            +- columns: ArrayList of columns
            |    |
            |    +- field
            |    +- getter
            |    +- setter

-->
<@pp.dropOutputFile />
<#list Entities as entity>
<@pp.changeOutputFile name="/development/${directory}/apis/rest/resources/${entity.name.plural}Resource.java" />

package ${rootPackage}.${applicationName}.apis.rest.resources;

import java.util.List;

import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

import ${rootPackage}.${applicationName}.apis.models.${entity.name.plural};
import ${rootPackage}.${applicationName}.apis.rest.DiagRestRepresentation;
import ${rootPackage}.${applicationName}.apis.rest.converters.${entity.name.plural}Converter;
import ${rootPackage}.${applicationName}.models.${entity.name.single};

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

public class ${entity.name.plural}Resource extends ServerResource {

    @Get("json")
    public Representation get${entity.name.plural}() {
        String s = getQuery().getFirstValue("start");
        String m = getQuery().getFirstValue("max");
        int start = (s == null) ? 0 : Integer.parseInt(s);
        int max = (m == null) ? 0 : Integer.parseInt(m);
        List<${entity.name.single}> list = new ${entity.name.plural}().Query(start, max,
            "SELECT e FROM ${entity.name.single} e");

        return new DiagRestRepresentation<${entity.name.plural}Converter>(
            MediaType.APPLICATION_JSON, new ${entity.name.plural}Converter(list));
    }

    @Post("json")
    public Representation Insert(String json) {
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.processAnnotations(${entity.name.plural}Converter.class);
        ${entity.name.plural}Converter converter = (${entity.name.plural}Converter) xstream.fromXML(json);

        List<${entity.name.single}> list = (List<${entity.name.single}>) converter.getEntities();

        ${entity.name.plural} controller = new ${entity.name.plural}();
        for (${entity.name.single} entity : list) {
        	Object id = controller.persist(entity);
        	entity.${entity.idColumn.setter}((${entity.idColumn.field.type})id);
        }
        return new DiagRestRepresentation<${entity.name.plural}Converter>(
                MediaType.APPLICATION_JSON, new ${entity.name.plural}Converter(list));
    }
}
</#list>